<template>
  <div>
    <lb-title></lb-title>
    <div class="page-main">
      <lb-tip
        >{{ $t('menu.Article') }}总数量: {{ totalNumber }}; 已分配数量:
        {{ useNumber }};</lb-tip
      >
      <el-row class="page-top-operate">
        <el-button type="primary" icon="el-icon-plus" @click="showDialog('add')"
          >新增授权</el-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="100px"> </el-table-column>
        <el-table-column prop="name" label="小程序名"> </el-table-column>
        <el-table-column prop="modular_id" label="小程序id"> </el-table-column>
        <el-table-column prop="number" label="获客文章数量"> </el-table-column>
        <el-table-column prop="used" label="已使用数量"> </el-table-column>
        <el-table-column prop="left" label="剩余数量"> </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="
                  showDialog('edit', scope.row.number, scope.row.modular_id)
                "
                >{{ $t('action.edit') }}</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="pageForm.page"
        :pageSize="pageForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <el-dialog
        :title="dailogTitle"
        :visible.sync="centerDialogVisible"
        width="480px"
        center
      >
        <el-form
          @submit.native.prevent
          :model="articleForm"
          :rules="articleFormRules"
          ref="articleForm"
          label-width="110px"
        >
          <el-form-item label="小程序" prop="modular_id">
            <el-select
              :disabled="oType === 'edit'"
              v-model="articleForm.modular_id"
              filterable
              clearable
              placeholder="请选择授权的小程序"
            >
              <el-option
                v-for="item in miappOptions"
                :key="item.modular_id"
                :label="item.mini_name"
                :value="item.modular_id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="获客文章数量" prop="number">
            <el-input-number
              class="lb-input-number"
              :min="0"
              :controls="false"
              style="
                 {
                  text-align: left;
                }
              "
              v-model="articleForm.number"
            ></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormInfo('articleForm')">{{
            $t('action.comfirm')
          }}</el-button>
          <el-button @click="centerDialogVisible = false">{{
            $t('action.cancel')
          }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      pageForm: {
        page: 1,
        page_count: 10
      },
      articleForm: {
        number: '',
        modular_id: ''
      },
      articleFormRules: {
        number: { required: true, type: 'number', message: '请输入数量', trigger: 'change' },
        modular_id: { required: true, type: 'number', message: '请选择小程序', trigger: 'blur' }
      },
      loading: false,
      total: 0,
      tableData: [],
      centerDialogVisible: false,
      miappOptions: [],
      value: '',
      totalNumber: 0,
      useNumber: 0,
      oType: 'add',
      dailogTitle: ''
    }
  },
  created () {
    this.getminiAppList()
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      this.$api.getArticleList(this.pageForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.totalNumber = res.data.total_article_number
          this.useNumber = res.data.total_article_used
          this.total = res.data.total
        }
      })
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let subForm = JSON.parse(JSON.stringify(this.articleForm))
          if (this.oType === 'edit') { // 编辑
            this.$api.editArticleAuthInfo(subForm).then(res => {
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successSub'))
                this.centerDialogVisible = false
                this.getTableDataList()
              }
            })
          } else { // 新增
            this.$api.addArticleAuthInfo(subForm).then(res => {
              if (res.code === 200) {
                this.$message.success(this.$t('tips.successSub'))
                this.centerDialogVisible = false
                this.getTableDataList()
              }
            })
          }
        }
      })
    },
    showDialog (type, number, id) {
      this.oType = type
      this.articleForm = type === 'add'
        ? {
          number: '',
          modular_id: ''
        }
        : {
          number: number,
          modular_id: id
        }
      this.dailogTitle = type === 'add' ? '新增授权' : '编辑授权'
      this.centerDialogVisible = true
    },
    getminiAppList () {
      this.$api.getAllMiniApp().then(res => {
        if (res.code === 200) {
          this.miappOptions = res.data
        }
      })
    },
    handleSizeChange (val) {
      this.pageForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.pageForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-inner {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
